package org.dromara.demo;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.codegen.template.impl.EnjoyTemplate;

import javax.sql.DataSource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class CustomGenerator extends Generator {

    private ITemplate template = new EnjoyTemplate();

    public CustomGenerator(DataSource dataSource, GlobalConfig globalConfig) {
        super(dataSource, globalConfig);
    }

    @Override
    public void generate(List<Table> tables) {
        if (tables == null || tables.isEmpty()) {
            System.err.printf("table %s not found.%n", globalConfig.getGenerateTables());
            return;
        } else {
            System.out.printf("find tables: %s%n", tables.stream().map(Table::getName).collect(Collectors.toSet()));
        }

        for (Table table : tables) {
            JavaGenTool javaGenTool = new JavaGenTool(table, "org.dromara.system");
            HashMap<String, Object> params = new HashMap<>();
            params.put("table", table);
            params.put("javaGenTool", javaGenTool);
            params.put("javadocConfig", globalConfig.getJavadocConfig());

            File entityJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getEntityPackagePath()) + "/" + javaGenTool.getEntityClassName() + ".java");
            template.generate(params, "/templates/entity.tpl", entityJavaFile);
            File boJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getBoPackagePath()) + "/" + javaGenTool.getBoClassName() + ".java");
            template.generate(params, "/templates/bo.tpl", boJavaFile);
            File qoJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getQoPackagePath()) + "/" + javaGenTool.getQoClassName() + ".java");
            template.generate(params, "/templates/qo.tpl", qoJavaFile);
            File voJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getVoPackagePath()) + "/" + javaGenTool.getVoClassName() + ".java");
            template.generate(params, "/templates/vo.tpl", voJavaFile);
            File mapperJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getMapperPackagePath()) + "/" + javaGenTool.getMapperClassName() + ".java");
            template.generate(params, "/templates/mapper.tpl", mapperJavaFile);
            File serviceJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getServicePackagePath()) + "/" + javaGenTool.getServiceClassName() + ".java");
            template.generate(params, "/templates/service.tpl", serviceJavaFile);
            File serviceImplJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getServiceImplPackagePath()) + "/" + javaGenTool.getServiceImplClassName() + ".java");
            template.generate(params, "/templates/serviceImpl.tpl", serviceImplJavaFile);
            File controllerJavaFile = new File(getSourceDir(), packagePath2FileUrl(javaGenTool.getControllerPackagePath()) + "/" + javaGenTool.getControllerClassName() + ".java");
            template.generate(params, "/templates/controller.tpl", controllerJavaFile);
            File mapperXmlFile = new File(getResourcesDir() + "mapper" + "/" + javaGenTool.getMapperClassName() + ".xml");
            template.generate(params, "/templates/mapperXml.tpl", mapperXmlFile);
        }
        System.out.println("Code is generated successfully.");
    }

    public String getSourceDir() {
        return System.getProperty("user.dir") + "/src/main/java/";
    }

    public String getResourcesDir() {
        return System.getProperty("user.dir") + "/src/main/resources/";
    }

    public String packagePath2FileUrl(String packagePath) {
        return packagePath.replace(".", "/");
    }
}
